/* FOR REFERENCE DURING DEV
function notification_item() {
	//private member variables
	var _whoName;
	var _whoImgSrc;
	var _what;
	var _link;
	var _gift;
	var _crewed;
	var _timestamp;
	var _text;
	
	//public member variables
	//that to make this available to private methods
	var that = this;
	
	//Property Sets
	this.setWho = function(whoName, whoImgSrc) { this._whoName = whoName; this._whoImgSrc = whoImgSrc; };
	this.setWhat = function(what) { this._what = what; };
	this.setLink = function(link) { 
		this._link = link;
		//try to pull the timestamp from the encoded URL
		var str = link;
		str = str.split('%22')[3];
		str = base64.decode(str);
		//it appears that I've been unable to find the exact base64 decode that zynga is using
		// however sometimes removing the last character after a decode seems to give me a valid JSON string
		while (String(str.substr(0, str.length - 1)).lastIndexOf('}') != -1) { str = str.substr(0, str.length - 1) }
		var obj = JSON.parse(str);
		if (obj.gift)
			this._gift = obj.gift;
		if (obj.buildingName)
			this._crewed = obj.buildingName;
		if (obj.timestamp)
			this._timestamp = obj.timestamp;
	};
	this.setText = function(text) { this._text = text; };
	
	//Property Gets
	this.what = function() { return this._what; };
	this.link = function() { return this._link; };
	this.whoName = function() { return this._whoName };
	this.whoImgSrc = function() { return this._whoImgSrc };
	this.time = function() { return new Date(this._timestamp*1e3); }
	this.gift = function() { return this._gift; }
	this.text = function() { return this._text; }
}*/



/* Gifts */
//BuildingPart265 = Vault Keys

/* chrome api alias' */
var tabs = chrome.tabs;
var storage = chrome.storage.local;

if(!Array.prototype.filter){Array.prototype.filter=function(e){var t=this.length;if(typeof e!="function")throw new TypeError;var n=new Array;var r=arguments[1];for(var i=0;i<t;i++){if(i in this){var s=this[i];if(e.call(r,s,i,this))n.push(s)}}return n}}

$(document).ready(function() {
	$(document).height = '250px';
	$('#stop').bind('click', function() {
		chrome.extension.sendMessage({"action": "stop"}, function(response) {
			//
		});
	});
	$('#close').bind('click', function() {
		window.close();
	});
	$('#refresh').bind('click', function() {
		chrome.extension.sendMessage({"action": "refresh"}, function(response) {
			window.close();
		});
	});
	$('#options').bind('click', function() {
		location.replace('options.html');
	});
	$(function() {
		$( "#tabs" ).tabs({
			event: "mouseover"
		});
	});
	
	loadNotes();
});

var joins = [];
var gifts = [];
var needs = [];

function loadNotes() {
	joins = [];
	gifts = [];
	var allNotes = [];
	var mysteryCount = 0;
	var lbCount = 0;
	
	var status = document.getElementById("status");
	status.innerHTML = "Loading Data...";
	
	storage.get('links', function(data) { 
		allNotes = data.links.slice(0);
	});
	
	setTimeout(function() {
		for (var i = 0; i<allNotes.length; i++) { 
			var item = allNotes[i];
			if (item) {
				if (item._crewed) {
					joins.push(item);
				} else {
					gifts.push(item);
				}
			} else {
				console.log('no item at ' + i);
			}
		} 
		
		var crewed = _.groupBy(joins, '_crewed');
		for (var i in crewed) {
			var count = crewed[i].length
			if (count > 0) {
				var j = (crew_items[i]) ? crew_items[i] : i;
				$('#joins').append('<tr><td>' + j + ' (' + count + ')</td></tr>');
			}
		}
		
		for (var i in req_items) {
			var count = 0;
			count = gifts.filter(function(item, index, array) { return item._gift === i; }).length;
			if (count > 0) {
				$('#reqs').append('<tr><td>' + req_items[i] + ' (' + count + ')</td></tr>');
			}
		}
		
		for (var i in gift_items) {
			var count = 0;
			count = gifts.filter(function(item, index, array) { return item._gift === i; }).length;
			if (count > 0) {
				$('#gifts').append('<tr><td>' + gift_items[i] + ' (' + count + ')</td></tr>');
			}
		}
		
		status.innerHTML = "";
	}, 2000);
}
